package com.jy.adscm.operation.base.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.base.bean.TaskManager;
import com.jy.adscm.operation.base.bean.TaskManagerinfo;
import com.jy.adscm.operation.base.mapper.mysql.TaskManagerinfoMapper;
import com.jy.adscm.operation.base.service.TaskManagerService;
import com.jy.adscm.operation.base.service.TaskManagerinfoService;
import com.jy.adscm.operation.hscrb.bean.LjzzPartsmadeinfo;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-12-11
 */
@Service
public class TaskManagerinfoServiceImpl extends ServiceImpl<TaskManagerinfoMapper, TaskManagerinfo> implements TaskManagerinfoService {
  @Autowired
    JyUserMapper jyUserMapper;
  @Autowired
    TaskManagerService  taskManagerService;
    @Override
    public List<TaskManagerinfo> selectByTid(String type, String tid) {
        Wrapper<TaskManagerinfo> wrapper =new EntityWrapper<>();
        wrapper.eq("TID",tid);
        wrapper.eq("type",type);
        List<TaskManagerinfo> TaskManagerinfos = super.selectList(wrapper);
        for (TaskManagerinfo taskManagerinfo : TaskManagerinfos) {
            if (taskManagerinfo.getPerson()!=null && !"".equals(taskManagerinfo.getPerson())){
                JyUser jyUser = jyUserMapper.selectById(taskManagerinfo.getPerson());
                taskManagerinfo.setPersonName(jyUser.getName());
            }
        }
        return TaskManagerinfos;
    }
    @Transactional
    @Override
    public boolean inserttaskManagerinfo(TaskManagerinfo taskManagerinfo, Token token) throws AdscmSystemException {
        TaskManager taskManager = taskManagerService.selectById(taskManagerinfo.getTid());
        if (taskManager.getPerformer().equals(String.valueOf(token.getUser().getId()))){
            taskManagerService.updateByPerformer(Integer.valueOf(taskManagerinfo.getTid()));
        }
        if (taskManager.getCreator().equals(String.valueOf(token.getUser().getId()))){
            taskManagerService.updateByCreator(Integer.valueOf(taskManagerinfo.getTid()));
        }
        boolean insert = super.insert(taskManagerinfo);
        return insert;
    }
    @Transactional
    @Override
    public boolean updateByTaskManagerinfo(TaskManagerinfo taskManagerinfo, Token token) {
        TaskManager taskManager = taskManagerService.selectById(taskManagerinfo.getTid());
        if (taskManager.getPerformer().equals(String.valueOf(token.getUser().getId()))){
            taskManagerService.updateByPerformer(Integer.valueOf(taskManagerinfo.getTid()));
        }
        if (taskManager.getCreator().equals(String.valueOf(token.getUser().getId()))){
            taskManagerService.updateByCreator(Integer.valueOf(taskManagerinfo.getTid()));
        }
        boolean b = super.updateById(taskManagerinfo);
        return b;
    }
}
